package com.zxc.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;

public class Test {

	public static void main(String[] args) {
//		System.out.println(Integer.toHexString(2723));
//		cancelPnr();
//		verifyLccPrice();
		
		String s = "QWERTYUIOPASDFGHJKLZXCVBNM";
        
        System.out.println(""+s.charAt(0)+s.charAt(2));
        
        s = "3K,3K,3M,3M,3U,4C,4M,4O,4O,4O,5J,5J,7C,7C,7C,8M,9C,9C,A3,AA,AB,AC,AD,AE,AF,AI,AK,AK,AM,AR,AS,AT,AV,AY,AZ,B0,B6,B6,B6,B7,BA,BE,BE,BE,BG,BI,BP,BR,BW,BX,BX,BX,CA,CI,CM,CX,CZ,D7,D7,DD,DD,DE,DG,DG,DL,DT,DX,DX,DY,DY,EI,EK,EQ,ET,EW,EW,EY,F9,F9,F9,FA,FA,FD,FD,FI,FJ,FM,FR,FR,FY,FY,G3,G4,G4,GA,GF,GK,GK,GP,H8,HA,HM,HO,HQ,HR,HU,HX,HY,I2,IB,ID,ID,IE,IG,IJ,IJ,IT,IT,IZ,J2,JD,JJ,JL,JM,JM,JP,JQ,JQ,JQ,JQ,JU,K6,KA,KC,KC,KC,KE,KL,KQ,KU,LA,LH,LJ,LJ,LJ,LO,LQ,LQ,LQ,LR,LU,LX,LY,MD,MF,MH,MI,MK,MM,MM,MR,MS,MT,MU,MX,MX,NH,NK,NK,NQ,NQ,NX,NZ,O6,OB,OD,OD,OD,OK,OM,OS,OZ,P9,PC,PC,PC,PD,PG,PR,PS,PX,PZ,Q9,Q9,QD,QF,QK,QK,QR,QV,QZ,QZ,RA,RJ,RK,RK,RV,RV,S4,S7,SA,SB,SC,SE,SK,SL,SL,SL,SN,SQ,SU,SV,SY,SY,TA,TB,TB,TB,TG,TK,TN,TP,TR,TR,TT,TT,TU,TW,TW,TX,TZ,U2,U2,U6,UA,UB,UL,UO,UO,UO,US,UU,UX,V7,V7,VA,VB,VB,VE,VJ,VJ,VN,VS,VW,VX,VY,VY,W4,W6,W6,WE,WE,WE,WN,WN,WN,WS,WY,XJ,XJ,XL,Y4,Y4,Y4,Z2,Z2,Z8,Z9,Z9,ZE,ZE,ZH,ZP";
        String[] arr = StringUtils.split(s, ",");
        List<String> l = CollectionUtil.newArrayList(arr).stream().sorted().distinct().collect(Collectors.toList());
        System.out.println(StringUtils.join(l, ","));
	}

	public static void cancelPnr() {
		List<String> list = FileUtil.readLines("classpath:cancelPnr", "UTF-8");
		
		for (String string : list) {
			System.out.println("请求参数："+string);
			String result = HttpUtil.post("http://travelportorder.fengxiangcloud.cn:9999/travelportOrder", string);
			System.out.println("响应结果："+result);
		}
	}
	
	public static void verifyLccPrice() {
		
		String from = "DEL";
		String to = "HKG";
		String flightNumber = "6E1707";
		double totalPrice1 = 0;
		double totalPrice2 = 0;
		double totalPrice3 = 0;
		while (true) {
			
			double tmpTotalPrice1 = verifyLccPrice(totalPrice1, from, to, "20231224",flightNumber);
			if(tmpTotalPrice1 != totalPrice1) {
				totalPrice1 = tmpTotalPrice1;
			}
			
			double tmpTotalPrice2 = verifyLccPrice(totalPrice2, from, to, "20231226",flightNumber);
			if(tmpTotalPrice2 != totalPrice2) {
				totalPrice2 = tmpTotalPrice2;
			}
			
			double tmpTotalPrice3 = verifyLccPrice(totalPrice3, from, to, "20231228",flightNumber);
			if(tmpTotalPrice3 != totalPrice3) {
				totalPrice3 = tmpTotalPrice3;
			}
			
			try {
				Thread.sleep(60*1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static double verifyLccPrice(double totalPrice,String from,String to ,String date,String flightNumber) {
		
		try {
			String pin = HttpUtil.get("https://api.xingliyuehui.com/pin/get/XX/f5Xv8gDw");
			
			String req = "{\"tt\":1,\"td\":\"\",\"pin\":\""+pin+"\",\"cn\":0,\"fc\":\""+from+"\",\"an\":1,\"fd\":\""+date+"\",\"tc\":\""+to+"\"}";
			
			String result = HttpUtil.post(" https://api.xingliyuehui.com/search/XX", req);
			
			JSONObject resp = JSONObject.parseObject(result);
			JSONArray data = resp.getJSONArray("data");
			
			for (Object object : data) {
				JSONObject o = (JSONObject) object;
				if(StringUtils.equals(flightNumber,o.getJSONArray("fl").getJSONObject(0).getString("fn"))) {
					double totalPrice1 = BigDecimal.valueOf(o.getDoubleValue("pr")+o.getDoubleValue("tx")).setScale(2, RoundingMode.HALF_UP).doubleValue();
					if(totalPrice != totalPrice1) {
						totalPrice = totalPrice1;
						System.out.println("抓取时间："+DateUtil.format(new java.util.Date(), DatePattern.NORM_DATETIME_FORMATTER)+",from="+from+",to="+to+",date="+date+",flightNumber="+flightNumber+",抓取价="+totalPrice1+",官网价=?");
						return totalPrice1;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return totalPrice;
	}
	
}
